package org.zerta.troops.core;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.zerta.troops.common.Action;
import org.zerta.troops.common.ActionList;
import org.zerta.troops.common.Area;
import org.zerta.troops.common.Army;
import org.zerta.troops.common.GameState;
import org.zerta.troops.common.TroopMovement;
import org.zerta.troops.common.Turn;
import org.zerta.troops.common.TurnData;
import org.zerta.troops.common.Unit;
import org.zerta.troops.server.ServerConsole;

public class TurnProcessor {

	private List<String> turnLog;
	private ServerConsole console;
	
	public GameState processTurn( Turn turn, ServerConsole console ) {
		GameState gameState = new GameState();
		turnLog = new ArrayList<String>();
		this.console = console;
		
		// Move phase
		Map <String, TurnData> turnDataMap = turn.getTurnDataList();
		for (TurnData turnData: turnDataMap.values()) {
			ActionList actionList = turnData.getActionList();
			for (	Iterator <Action> actionListIterator = actionList.getActionList().iterator(); 
					actionListIterator.hasNext(); 
					) {
				Action action = actionListIterator.next();
				if (action.getType().equalsIgnoreCase("TroopMovement")) {
					TroopMovement troopMovement = (TroopMovement) action;
					// Do troop movement
					if (turnData.getPlayer().getName().equals(troopMovement.getOrigin().getOwner().getName())) {
						// You own the land. Ready to move!
						Area origin = troopMovement.getOrigin();
						Area destination = troopMovement.getDestination();
						Army army = origin.getOwnerArmy();
						Unit unit = troopMovement.getUnit();
						int numInOrigin = army.getArmyUnit(unit);
						int numToMove = troopMovement.getNum();
						int numToMoveResult = 0;
						if (numInOrigin > numToMove) {
							numToMoveResult = numInOrigin;
						} else if (numInOrigin == numToMove) {
							numToMoveResult = numToMove;
						} else {
							// Error: trying to move too many
							if (numInOrigin > 0) {
								numToMoveResult = numInOrigin;
							} else {
								numToMoveResult = 0;
							}
						}
						army.removeArmyUnit(unit, numToMoveResult);
						Army newArmy = destination.getCurrentArmyForPlayer(turnData.getPlayer());
						if (newArmy == null) {
							newArmy = new Army();
							newArmy.setOwner(turnData.getPlayer());
						}
						newArmy.addArmyUnit(unit, numToMoveResult);
						turnLog.add("Player " + turnData.getPlayer().getName() 
								+ " moved " + (numToMoveResult) + unit.getName() 
								+ " from " + origin.getName()
								+ " to " + destination.getName()
								);
						destination.addCurrentArmy(newArmy);
					} else {
						// Trying to move an army away from a land you you do not control. Bad dog!
					}
				}
			}
		}
		
		BattleEngine battleEngine = new BattleEngine();
		
		// Battle phase
		for (Area area: turn.getGame().getMap().getAreas().values() ) {
			battleEngine.executeBattle(area, this);
		}
		
		gameState.setGame(turn.getGame());
		return gameState;
	}
	
	public void log( String message ) {
		console.log(message);
		turnLog.add(message);
	}
}
